package architecture.demo12;



import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.List;
import java.util.Scanner;

public class Client {
    private static final String SERVER_IP = "127.0.0.1";
    private static final int SERVER_PORT = 1234;

    public static void main(String[] args) {
        try (Socket socket = new Socket(SERVER_IP, SERVER_PORT);
             ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
             ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream())) {

            Scanner scanner = new Scanner(System.in);
            boolean isExit = false;

            while (!isExit) {
                // 提示用户输入命令
                System.out.print("请输入命令：");
                String command = scanner.nextLine();

                // 发送命令给服务器
                outputStream.writeUTF(command);
                outputStream.flush();

                switch (command) {
                    case "add":
                        System.out.print("请输入联系人信息（姓名 地址 电话）：");
                        String contactInfo = scanner.nextLine();
                        outputStream.writeUTF(contactInfo);
                        outputStream.flush();

                        // 接收添加结果
                        String addResult = inputStream.readUTF();
                        System.out.println(addResult);
                        break;

                    case "search":
                        System.out.print("请输入关键词：");
                        String keyword = scanner.nextLine();
                        outputStream.writeUTF(keyword);
                        outputStream.flush();

                        // 接收搜索结果
                        List<Contact> searchResults = (List<Contact>) inputStream.readObject();
                        if (searchResults.isEmpty()) {
                            System.out.println("没有找到符合条件的联系人。");
                        } else {
                            System.out.println("搜索结果：");
                            for (Contact c : searchResults) {
                                System.out.println(c);
                            }
                        }
                        break;

                    case "update":
                        System.out.print("请输入联系人信息和当前姓名（姓名 地址 电话 当前姓名）：");
                        String updateInfo = scanner.nextLine();
                        outputStream.writeUTF(updateInfo);
                        outputStream.flush();

                        // 接收修改结果
                        String updateResult = inputStream.readUTF();
                        System.out.println(updateResult);
                        break;

                    case "delete":
                        System.out.print("请输入要删除的联系人姓名：");
                        String deleteName = scanner.nextLine();
                        outputStream.writeUTF(deleteName);
                        outputStream.flush();

                        // 接收删除结果
                        String deleteResult = inputStream.readUTF();
                        System.out.println(deleteResult);
                        break;

                    case "exit":
                        isExit = true;
                        break;

                    default:
                        System.out.println("无效命令。");
                        break;
                }
            }

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
